package com.zzedu.web.controller.monitor;

import com.zzedu.common.annotation.Log;
import com.zzedu.common.core.controller.BaseController;
import com.zzedu.common.core.domain.AjaxResult;
import com.zzedu.common.core.page.TableDataInfo;
import com.zzedu.common.core.text.Convert;
import com.zzedu.common.enums.BusinessType;
import com.zzedu.system.domain.SysUserOnline;
import com.zzedu.system.service.ISysUserOnlineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.session.data.redis.RedisSessionRepository;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 在线用户监控
 *
 * @author smarthome
 */
@RestController
@RequestMapping("/monitor/online")
public class SysUserOnlineController extends BaseController {

    @Autowired
    private RedisSessionRepository redisIndexedSessionRepository;

    @Autowired
    private ISysUserOnlineService userOnlineService;

    @PreAuthorize("@permission.hasAnyPermissions('monitor:online:list')")
    @RequestMapping("/list")
    public TableDataInfo list(@RequestBody SysUserOnline userOnline) {
        return getDataTable(userOnlineService.selectUserOnlineList(userOnline.getPageDTO(), userOnline));
    }

    @Log(title = "在线用户", businessType = BusinessType.FORCE)
    @RequestMapping("/{tokenId}")
    public AjaxResult batchForceLogout(@PathVariable String[] tokenId) {
        for (String sid : tokenId) {
            userOnlineService.deleteOnlineById(sid);
            try {
                redisIndexedSessionRepository.deleteById(sid);
            } catch (Exception e) {

            }

        }
        return success();
    }
}
